package com.zhihaoliang.coroutine.buss.net.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.zhihaoliang.coroutine.buss.net.model.User
import com.zhihaoliang.coroutine.databinding.NetLayoutUserItemBinding

/**
 * @author 支豪亮
 * @date 2022/10/14 15:29
 * @des
 */
class UserAdapter : RecyclerView.Adapter<UserViewHolder>() {

    private val userList: MutableList<User> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val dataBinding = NetLayoutUserItemBinding.inflate(layoutInflater, parent, false)
        return UserViewHolder(dataBinding)
    }

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        holder.itemBinding.user = userList[position]
    }

    override fun getItemCount(): Int = userList.size

    @SuppressLint("NotifyDataSetChanged")
    fun setData(list: List<User>) {
        if (userList.isNotEmpty()) userList.clear()
        userList.addAll(list)
        notifyDataSetChanged()
    }

}

class UserViewHolder(val itemBinding: NetLayoutUserItemBinding) :
    RecyclerView.ViewHolder(itemBinding.root)